package vn.egame.elockscreen.view;

import vn.egame.elockscreen.ELockscreenPreferences;
import vn.egame.elockscreen.OnUnlockListener;
import vn.egame.elockscreen.R;
import vn.egame.elockscreen.activity.LockScreenAnimationActivity;
import vn.egame.elockscreen.activity.PasscodeActivity;
import vn.egame.elockscreen.object.Animation;
import vn.egame.elockscreen.object.Texture;
import vn.egame.elockscreen.utils.Constans;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.drm.DrmStore.RightsStatus;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.provider.SyncStateContract.Helpers;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.HeaderViewListAdapter;

public class LockscreenAniamtionView extends SurfaceView implements
		SurfaceHolder.Callback, OnTouchListener {

	public static final int READ_FROM_JAR_SDCARD = 1;
	public static final int READ_FROM_FORDER_SDCARD = 2;
	public static final int READ_FROM_ASSET = 3;

	private Bitmap mBackground;

	private Activity activity;
	private Context mContext;

	private int width, height;
	private OnUnlockListener unlockListener;

	private Paint mPaint;

	public static final int STATE_WAIT = 1;
	public static final int STATE_JUMP = 2;
	public static final int STATE_BACK = 3;
	public static final int STATE_STOP = 4;
	public static final int STATE_UNLOCK = 5;
	private int state;

	private Texture[] textures;
	private int[] columns;

	int posStart, posStop;
	int posHeight;
	int up, left;

	private boolean canMoveBack;
	private Animation aWait, aDrag, aBack, aStop, aOther, aOtherStop;

	int typeRead;

	public LockscreenAniamtionView(Context context) {
		super(context);
		init(context);
	}

	public LockscreenAniamtionView(Context context, AttributeSet attrSet) {
		super(context, attrSet);
		init(context);
	}

	public void init(Context context) {
		// ELockscreenPreferences.getInstance().initPreferences(context);

		this.mPaint = new Paint();
		this.mPaint.setColor(-1);

		mContext = context;

		WindowManager wm = (WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE);

		DisplayMetrics displaymetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(displaymetrics);
		height = displaymetrics.heightPixels;
		width = displaymetrics.widthPixels;

		if (width > height) {
			int temp = width;
			width = height;
			height = temp;
		}
		state = STATE_WAIT;

		String temp;
		typeRead = READ_FROM_ASSET;
		switch (typeRead) {
		case READ_FROM_JAR_SDCARD:
			
			Log.d("Kai", ELockscreenPreferences
					.getInstance().getPathLockTheme() );
			String c = Constans.parseJsonFromZip(ELockscreenPreferences
					.getInstance().getPathLockTheme(), "config.txt");// "2 1 1 1";

			
			String[] colum = c.split(" ");

			posStart = (int) (width * Integer.parseInt(colum[7]) / 100);
			posStop = (int) (width * Integer.parseInt(colum[8]) / 100);
			posHeight = (int) (height * Integer.parseInt(colum[9]) / 100);

			temp = colum[10];
			if ((colum[10].charAt(0) + "").equals("0")) {
				up = -Integer.parseInt(temp.substring(0));
			} else {
				up = Integer.parseInt(temp.substring(0));
			}

			temp = colum[11];
			if ((colum[11].charAt(0) + "").equals("0")) {
				left = -Integer.parseInt(temp.substring(1));
			} else {
				left = Integer.parseInt(temp.substring(1));
			}

			columns = new int[colum.length];
			for (int i = 0; i < colum.length; i++) {
				columns[i] = Integer.parseInt(colum[i].toString());
			}
			String zipPathFile = ELockscreenPreferences.getInstance()
					.getPathLockTheme();

			this.mBackground = Constans.getBtimapFromZip(zipPathFile, "bg.png");

			if (!(colum[1] + "").equals("0")) {
				aWait = new Animation(new Texture(context, zipPathFile,
						"EV_1.png"), 1, columns[1], 200);
				aWait.setPosition(posStart - aWait.getWidthFrame() / 2,
						posHeight - aWait.getHeight() / 2);
			}

			if (!(colum[2] + "").equals("0")) {
				aDrag = new Animation(new Texture(context, zipPathFile,
						"EV_2.png"), 1, columns[2], 200);
			}

			if (!(colum[3] + "").equals("0")) {
				aBack = new Animation(new Texture(context, zipPathFile,
						"EV_3.png"), 1, columns[3], 200);
			}

			if (!(colum[4] + "").equals("0")) {
				aStop = new Animation(new Texture(context, zipPathFile,
						"EV_4.png"), 1, columns[4], 200);
			}

			if (!(colum[5] + "").equals("0")) {
				aOther = new Animation(new Texture(context, zipPathFile,
						"EV_5.png"), 1, columns[5], 200);
				aOther.setPosition(posStop, posHeight - aWait.getHeight() / 2);
			}

			if (!(colum[6] + "").equals("0")) {
				aOtherStop = new Animation(new Texture(context, zipPathFile,
						"EV_6.png"), 1, columns[6], 200);
				aOtherStop.setPosition(posStop, posHeight - aWait.getHeight()
						/ 2);
			}

			// columns = new int[colum.length];
			//
			// for (int i = 0; i < colum.length; i++) {
			// columns[i] = Integer.parseInt(colum[i].toString());
			// }

			// String zipPathFile = ELockscreenPreferences.getInstance()
			// .getPathLockTheme();
			//
			// aWait = new Animation(new Texture(context, zipPathFile, "1.png"),
			// 1, columns[0], 500);
			// aWait.setPosition(130, 960);
			// aDrag = new Animation(new Texture(context, zipPathFile, "2.png"),
			// 1, columns[1], 500);
			// aBack = new Animation(new Texture(context, zipPathFile, "3.png"),
			// 1, columns[2], 500);
			// aStop = new Animation(new Texture(context, zipPathFile, "4.png"),
			// 1, columns[3], 500);
			break;

		case READ_FROM_FORDER_SDCARD:
			break;
		case READ_FROM_ASSET:

			c = "1 7 3 3 1 6 1 15 75 75 030 020";

			colum = c.split(" ");

			posStart = (int) (width * Integer.parseInt(colum[7]) / 100);
			posStop = (int) (width * Integer.parseInt(colum[8]) / 100);
			posHeight = (int) (height * Integer.parseInt(colum[9]) / 100);

			temp = colum[10];
			if ((colum[10].charAt(0) + "").equals("0")) {
				up = -Integer.parseInt(temp.substring(0));
			} else {
				up = Integer.parseInt(temp.substring(0));
			}

			temp = colum[11];
			if ((colum[11].charAt(0) + "").equals("0")) {
				left = -Integer.parseInt(temp.substring(1));
			} else {
				left = Integer.parseInt(temp.substring(1));
			}

			Log.d("size", up + " " + left);

			columns = new int[colum.length];
			for (int i = 0; i < colum.length; i++) {
				columns[i] = Integer.parseInt(colum[i].toString());
			}
			zipPathFile = "data/";

			this.mBackground = Constans.getBitmapFromAsset(getContext(),
					zipPathFile + "bg.png");

			if (!(colum[1] + "").equals("0")) {
				aWait = new Animation(new Texture(context, zipPathFile,
						"EV_1.png"), 1, columns[1], 200);
				aWait.setPosition(posStart - aWait.getWidthFrame() / 2,
						posHeight - aWait.getHeight() / 2);
			}

			if (!(colum[2] + "").equals("0")) {
				aDrag = new Animation(new Texture(context, zipPathFile,
						"EV_2.png"), 1, columns[2], 200);
			}

			if (!(colum[3] + "").equals("0")) {
				aBack = new Animation(new Texture(context, zipPathFile,
						"EV_3.png"), 1, columns[3], 200);
			}

			if (!(colum[4] + "").equals("0")) {
				aStop = new Animation(new Texture(context, zipPathFile,
						"EV_4.png"), 1, columns[4], 200);
			}

			if (!(colum[5] + "").equals("0")) {
				aOther = new Animation(new Texture(context, zipPathFile,
						"EV_5.png"), 1, columns[5], 200);
				aOther.setPosition(posStop, posHeight - aWait.getHeight() / 2);
			}

			if (!(colum[6] + "").equals("0")) {
				aOtherStop = new Animation(new Texture(context, zipPathFile,
						"EV_6.png"), 1, columns[6], 200);
				aOtherStop.setPosition(posStop, posHeight - aWait.getHeight()
						/ 2);
			}

			break;
		}

		setWillNotDraw(false);
		setOnTouchListener(this);
	}

	public void update() {

		if (aOther != null) {
			aOther.update();
		}
		switch (state) {
		case STATE_WAIT:
			aWait.update();
			break;

		case STATE_JUMP:
			aDrag.update();
			break;
		case STATE_BACK:
			aBack.update();
			break;
		case STATE_STOP:
			aStop.update();
			break;
		case STATE_UNLOCK:

			break;
		}

	}

	private boolean tien = true;

	@SuppressLint("WrongCall")
	public void drawUpdate(Canvas canvas) {
		canvas.drawBitmap(
				this.mBackground,
				new Rect(0, 0, this.mBackground.getWidth(), this.mBackground
						.getHeight()), new Rect(0, 0, getWidth(), getHeight()),
				this.mPaint);

		switch (state) {
		case STATE_WAIT:
			aWait.onDraw(canvas, aWait.getPosX(), aWait.getPosY());
			if (aOther != null) {
				aOther.onDraw(canvas, aOther.getPosX(), aOther.getPosY());
			}
			break;

		case STATE_JUMP:
			aDrag.onDraw(canvas, aDrag.getPosX(), aDrag.getPosY());
			if (aOther != null) {
				aOther.onDraw(canvas, aOther.getPosX(), aOther.getPosY());
			}
			break;
		case STATE_BACK:
			aBack.onDraw(canvas, aBack.getPosX(), aBack.getPosY());
			if (aOther != null) {
				aOther.onDraw(canvas, aOther.getPosX(), aOther.getPosY());
			}
			break;
		case STATE_STOP:
			aStop.onDraw(canvas, aStop.getPosX(), aStop.getPosY());

			if (aOtherStop != null) {
				aOtherStop.onDraw(canvas, aOtherStop.getPosX(),
						aOtherStop.getPosY());
			}
			if (System.currentTimeMillis() - timeCurrent > 300) {
				state = STATE_UNLOCK;

				// if (ELockscreenPreferences.getInstance().isEnablePasscode())
				// {
				//
				// ((LockScreenAnimationActivity) getContext())
				// .onConfirmPasscode();
				//
				// } else {
				// unlockListener.onUnlock();
				// }

				unlockListener.onUnlock();

			}
			break;
		case STATE_UNLOCK:
			aStop.onDraw(canvas, aStop.getPosX(), aStop.getPosY());
			if (aOtherStop != null) {
				aOtherStop.onDraw(canvas, aOtherStop.getPosX(),
						aOtherStop.getPosY());
			}
			break;
		}

		// aDemo.onDraw(canvas, aWait.getPosX() + 200, aWait.getPosY());

	}

	@Override
	protected void onDraw(Canvas canvas) {

		update();
		drawUpdate(canvas);

		invalidate();
		super.onDraw(canvas);
	}

	boolean isTouch;
	int posX, posY, oldPosX;
	long timeCurrent;

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:

			posX = (int) event.getX();
			posY = (int) event.getY();
			if (aWait.isCollition(posX, posY)) {
				state = STATE_JUMP;
				aDrag.setPosition(aWait.getPosX(), aWait.getPosY() + up);
			}
			switch (state) {
			case STATE_WAIT:

				break;

			case STATE_JUMP:

				break;
			case STATE_BACK:

				break;
			case STATE_STOP:

				break;
			case STATE_UNLOCK:

				break;
			}

			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:

			switch (state) {
			case STATE_WAIT:

				break;

			case STATE_JUMP:
				posX = (int) event.getX();

				if (posX - posStart > 30) {
					canMoveBack = true;
				}
				if (posX > oldPosX) {
					aDrag.setPosX(posX - aDrag.getWidthFrame() / 2);
					if (posX > posStop - aDrag.getWidthFrame() / 2 + left) {
						state = STATE_STOP;
						aStop.setPosition(posStop - aDrag.getWidthFrame() / 2
								+ left, aWait.getPosY());
						timeCurrent = System.currentTimeMillis();
					}
					oldPosX = posX;
				} else if (canMoveBack && oldPosX - posX > 3) {
					state = STATE_BACK;
					aBack.setPosition(aDrag.getPosX(), aDrag.getPosY());
				}

				break;
			case STATE_BACK:
				posX = (int) event.getX();
				if (posX - oldPosX > 3) {
					state = STATE_JUMP;
					aDrag.setPosition(aBack.getPosX(), aBack.getPosY());
				} else {

					aBack.setPosX(posX - aDrag.getWidthFrame() / 2);
					if (posX > posStop - aDrag.getWidthFrame() / 2) {
						state = STATE_STOP;
						aStop.setPosition(posX, aWait.getPosY());
						timeCurrent = System.currentTimeMillis();
					}
					oldPosX = posX;
				}

				break;
			case STATE_STOP:

				break;
			case STATE_UNLOCK:

				break;
			}

			invalidate();
			break;
		case MotionEvent.ACTION_UP:

			canMoveBack = false;
			posX = 0;
			oldPosX = 0;
			switch (state) {
			case STATE_WAIT:

				break;

			case STATE_JUMP:
				state = STATE_WAIT;
				aWait.setPosition(posStart - aWait.getWidthFrame() / 2,
						aWait.getPosY());
				break;
			case STATE_BACK:
				state = STATE_WAIT;
				aWait.setPosition(posStart - aWait.getWidthFrame() / 2,
						aWait.getPosY());
				break;
			case STATE_STOP:

				break;
			case STATE_UNLOCK:

				break;
			}
			invalidate();
			break;
		}

		return true;
	}

	public void resetState() {
		state = STATE_WAIT;
		aWait.setPosition(posStart - aWait.getWidthFrame() / 2, aWait.getPosY());
	}

	public OnUnlockListener getUnlockListener() {
		return unlockListener;
	}

	public void setUnlockListener(OnUnlockListener unlockListener) {
		this.unlockListener = unlockListener;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return true;

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}

}
